package com.zhoug.common3.filters;

import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;

/**
 * 中文逗号转化为英文逗号
 * @Author: zhoug
 * @Date: 2023-11-02
 * @Description:
 */
public class CNToENCommaInputFilter implements InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (source != null) {
            boolean isOriginal = true;//过滤规则是否改变
            String s = source.toString();
            if (s.contains("，")) {
                isOriginal = false;
                s = s.replaceAll("，", ",");
            }
            if (isOriginal) {
                return null;
            }
            if (source instanceof Spanned) {
                SpannableString sp = new SpannableString(s);
                TextUtils.copySpansFrom((Spanned) source, start, s.length(), null, sp, 0);
                return sp;
            } else {
                return s;
            }

        }
        return null;
    }
}
